<template>
  <!-- sync是双向的 -->
  <!-- dialogVisible是父组件传进来的数据，不是自己的数据 -->
  <el-dialog
    :title="title"
    :visible="dialogVisible"
    width="50%"
    @close="handleClose"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- <el-input v-model.trim="form.manager" /> -->
        <el-select v-model.trim="form.manager" placeholder="请选择负责人" clearable>
          <el-option v-for="item in simpleEmployeeList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model.trim="form.introduce" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button size="small" @click="handleClose">取消</el-button>
      <el-button type="primary" size="small" @click="handleClick">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { addDepartments, updateDepartments } from '@/api/departments'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      required: true
    },
    rawDepts: {
      type: Array,
      default: () => []
    },
    pid: {
      type: String,
      required: true
    },
    simpleEmployeeList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    // 登录功能 自定义校验规则
    // value永远是最新的
    // pid 父级的id
    // filter,find,findIndex,some,every,reduce
    // for(var i = ) forEach
    const validName = (rule, value, callback) => {
      // 编辑的时候 如果和自己一样可以，不能和兄弟一样
      let arr = this.rawDepts.filter(item => item.pid === this.pid)
      if (this.title === '编辑部门') {
        // 把自己排除 因为在编辑的时候可以和自己同名 因为id不会变的
        arr = arr.filter(item => item.id !== this.form.id)
      }
      // 只要用户输入的部门名称和数组中某一个一样就匹配了
      if (arr.some(item => item.name === value)) {
        callback(new Error('同级部门下已经有' + value + '这个部门'))
      } else {
        callback()
      }
    }

    const validCode = (rule, value, callback) => {
      let arr = this.rawDepts.filter(item => item.pid === this.pid)
      if (this.title === '编辑部门') {
        arr = arr.filter(item => item.id !== this.form.id)
      }
      // 只要用户输入的部门名称和数组中某一个一样就匹配了
      if (arr.some(item => item.code === value)) {
        callback(new Error('同级部门下已经有' + value + '这个编码'))
      } else {
        callback()
      }
    }

    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在1-50之间', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在1-50之间', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 300, message: '长度在1-300之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
      this.$refs.form.resetFields()
    },
    async handleClick() {
      // 数据校验
      try {
        await this.$refs.form.validate()
        this.form.pid = this.pid
        if (this.title === '新增部门') {
          await addDepartments(this.form)
          this.$message.success('添加部门成功')
        } else {
          await updateDepartments(this.form)
          this.$message.success('编辑部门成功')
        }
        // 对话框关闭
        this.$emit('close')
        this.$emit('update')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-dialog__footer {
  text-align: center;
}
</style>
